package com.agileai.pm.module.base.handler;

import java.util.List;

import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.annotation.PageAction;
import com.agileai.hotweb.controller.core.StandardListHandler;
import com.agileai.hotweb.domain.FormSelectFactory;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.RedirectRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.pm.module.base.service.TeamStaffManage;
import com.agileai.util.ListUtil;

public class TeamStaffManageListHandler
        extends StandardListHandler {
    public TeamStaffManageListHandler() {
        super();
        this.editHandlerClazz = TeamStaffManageEditHandler.class;
        this.serviceId = buildServiceId(TeamStaffManage.class);
    }
    
	public ViewRenderer prepareDisplay(DataParam param){
		mergeParam(param);
		initParameters(param);
		this.setAttributes(param);
		List<DataRow> rsList = getService().findRecords(param);
		this.setRsList(rsList);
		processPageAttributes(param);
		return new LocalRenderer(getPage());
	}

    protected void processPageAttributes(DataParam param) {
        initMappingItem("USER_SEX",
                        FormSelectFactory.create("USER_SEX").getContent());
        initMappingItem("USER_TYPE",
                FormSelectFactory.create("USER_TYPE").getContent());
    }

    protected void initParameters(DataParam param) {
        initParamItem(param, "userName", "");
        initParamItem(param, "userCode", "");
    }
    
    @PageAction
	public ViewRenderer refresh(DataParam param) {
		return prepareDisplay(param);
	}
	
    
    protected TeamStaffManage getService() {
        return (TeamStaffManage) this.lookupService(this.getServiceId());
    }
    
	public ViewRenderer doDeleteAction(DataParam param){
		getService().deleteStaffInfo(param);
		String isLast = param.get("isLast");
		if("true".equals(isLast)){
			getService().delectGrp(param);
			String userId = param.get("userId");
			getService().deleteUserRole(userId,"67E9AE43-0967-4943-9740-104ED1EB4D2A");
			getService().deletRecord(param);
		}
		return new RedirectRenderer(getHandlerURL(getClass()));
	}
	@PageAction
	public ViewRenderer checkDelete(DataParam param){
		String responseText = SUCCESS;
		storeParam(param);
		String userId = param.get("USER_ID");
		List<DataRow> userList = getService().findUserProjectRelRecords(userId);
		if(ListUtil.isNullOrEmpty(userList)){
			responseText = "last";
		}else if(userList.size()==1){
			responseText = "last";
		}
		return new AjaxRenderer(responseText);
	}
}
